package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xin.di.uav.applet.mapper.BusinessMapper;
import com.xin.di.uav.applet.mapper.DictionaryMapper;
import com.xin.di.uav.applet.mapper.ProductMapper;
import com.xin.di.uav.applet.resp.GoodsResponse;
import com.xin.di.uav.applet.resp.ShopCartDetailsResponse;
import com.xin.di.uav.applet.service.HomePageService;
import com.xin.di.uav.common.enums.SubjectEnum;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.mapper.SpecificationDao;
import com.xin.di.uav.operate.mapper.CustomDeliveryPointMapper;
import com.xin.di.uav.operate.po.DeliveryPointPO;
import com.xin.di.uav.operate.po.PlacementPoint;
import com.xin.di.uav.po.GoodsMapper;
import com.xin.di.uav.po.SpecificationPO;
import com.xin.di.uav.user.order.po.MerchantPO;
import com.xin.di.uav.user.order.po.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private CustomDeliveryPointMapper customDeliveryPointMapper;

    @Autowired
    private SpecificationDao specificationDao;

    @Override
    public GoodsResponse goods(String placementPointId) {

        GoodsResponse goodsResponse = new GoodsResponse();
        String txt = dictionaryMapper.selJsonString(SubjectEnum.TOP_NOTICE_TXT.getCode());
        goodsResponse.setTopNoticeTxt(txt);
        PlacementPoint placementPoint = customDeliveryPointMapper.selectById(placementPointId);
        if (placementPoint == null) {
            throw new BusinessException("投放点不存在");
        }

        goodsResponse.setDeliveryPointName(placementPoint.getName());
        goodsResponse.setExpressFee(placementPoint.getExpressFee());
        goodsResponse.setExpressDuration(placementPoint.getExpressDuration());
        Integer status = placementPoint.getStatus();
        if (status != 1) {
            throw new BusinessException("投放点已关闭");
        }
        String scenicSpotId = placementPoint.getScenicSpotId();
        if (ObjectUtil.isEmpty(scenicSpotId)) {
            throw new BusinessException("景区id不能为空");
        }


        List<MerchantPO> merchantPOS = businessMapper.selectListOrderBySort(1, 10, scenicSpotId);
        if (CollectionUtil.isEmpty(merchantPOS)) {
            throw new BusinessException("没有营业的商家");
        }
        if (CollectionUtil.isNotEmpty(merchantPOS)) {
            List<GoodsResponse.BusinessResponse> businessResponses = new ArrayList<>();

            List<String> merchantIds = merchantPOS.stream().map(MerchantPO::getId).collect(Collectors.toList());
            List<Product> products = productMapper.selectListOrderBySort(0, 10, merchantIds);

            for (MerchantPO merchantPO : merchantPOS) {
                GoodsResponse.BusinessResponse businessResponse = BeanUtil.copyProperties(merchantPO, GoodsResponse.BusinessResponse.class);
                List<Product> poList = products.stream().filter(productPO -> productPO.getMerchantId().equals(merchantPO.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(poList)) {
                    List<GoodsResponse.GoodsInfoResponse> goodsResponses = new ArrayList<>();
                    List<String> goodIds = poList.stream().map(Product::getId).collect(Collectors.toList());
                    List<SpecificationPO> specificationPOS = specificationDao.selByProductIds(goodIds);
                    for (Product product : poList) {
                        GoodsResponse.GoodsInfoResponse goodsInfoResponse = BeanUtil.copyProperties(product, GoodsResponse.GoodsInfoResponse.class);
                        List<SpecificationPO> specificationPOS1 = specificationPOS.stream().filter(specificationPO -> specificationPO.getProductId().equals(product.getId())).collect(Collectors.toList());
                        if (CollectionUtil.isNotEmpty(specificationPOS1)) {
                            List<GoodsResponse.SpecificationResp> specificationRespList = BeanUtil.copyProperties(specificationPOS1, GoodsResponse.SpecificationResp.class);
                            goodsInfoResponse.setSpecificationRespList(specificationRespList);
                            goodsInfoResponse.setSalePrice(specificationPOS1.get(0).getSalePrice());
                            int totalStock = specificationPOS1.stream().mapToInt(SpecificationPO::getStock).sum();
                            goodsInfoResponse.setStock(totalStock);
                        }
                        goodsResponses.add(goodsInfoResponse);
                    }
                    businessResponse.setGoodsResponseList(goodsResponses);
                    businessResponses.add(businessResponse);
                }

            }
            goodsResponse.setBusinessResponseList(businessResponses);

        }
        return goodsResponse;
    }

    @Override
    public ShopCartDetailsResponse shopCart(String productId) {
        Product product = goodsMapper.selectById(productId);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        ShopCartDetailsResponse shopCartDetailsResponse = BeanUtil.copyProperties(product, ShopCartDetailsResponse.class);
        List<SpecificationPO> specificationPOS = specificationDao.selByProductIds(Collections.singletonList(product.getId()));
//        List<ShopCartDetailsResponse.SpecificationResp> specificationRespList = BeanUtil.copyProperties(specificationPOS, ShopCartDetailsResponse.SpecificationResp.class);
        String txt = dictionaryMapper.selJsonString(SubjectEnum.SPECIFICATION_TITLE.getCode());
        if (ObjectUtil.isNotEmpty(txt)) {
            List<String> specificationsTitle = Arrays.asList(txt.split(","));
            shopCartDetailsResponse.setSpecificationsTitle(specificationsTitle);
        }
//        shopCartDetailsResponse.setSpecificationRespList(specificationRespList);
        return shopCartDetailsResponse;
    }
}
